package jungTest;

import java.awt.Dimension;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JFrame;

import org.apache.commons.collections15.Factory;

import processing.core.PApplet;
import wpRelations.TsvTable;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.event.GraphEvent.Vertex;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.samples.SimpleGraphDraw;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;


public class JungTest_001 extends PApplet{
	SparseMultigraph<String,Integer> myGraph;
	TsvTable testTable;
	String fileName = "Coby_Miller_links.tsv";
	
	/**
	 * @param args
	 */
	public void setup() {
		size(400,400);
		testTable = new TsvTable(this, fileName);
		//should maybe use a more general type for fine-tuning constraints?
		myGraph = new SparseMultigraph<String,Integer>();
		
		System.out.println("The table " + fileName + " has " + testTable.getRowCount() + " rows.");
		System.out.println("The table " + fileName + " has " + testTable.data[0].length + " coloumns.");
		for (int i = 0; i < testTable.getRowCount(); i++) {
			try {
				myGraph.addEdge((Integer)i, testTable.getString(i,0),testTable.getString(i,1),EdgeType.DIRECTED);	
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println(e);
			}
			
		}
		//System.out.println("The graph myGraph = " + myGraph.toString());
		
		//SimpleGraphDraw sgv = new SimpleGraphView(); //We create our graph in here
		// The Layout<V, E> is parameterized by the vertex and edge types
		Layout<Integer, String> layout = new CircleLayout(myGraph);
		layout.setSize(new Dimension(800,800)); // sets the initial size of the space
//		Factory<Graph<String, Integer>> myFactory = myGraph.getFactory();
//		for (int i = 0; i < layout.getGraph().getVertexCount(); i++) {
//			System.out.println(layout.transform(vertices.get(i)).toString());
//		}
		// The BasicVisualizationServer<V,E> is parameterized by the edge types
		BasicVisualizationServer<Integer,String> vv =
		new BasicVisualizationServer<Integer,String>(layout);
		vv.setPreferredSize(new Dimension(350,350)); //Sets the viewing area size
		JFrame frame = new JFrame("Simple Graph View");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(vv);
		frame.pack();
		frame.setVisible(true);
		Point2D p2d = layout.transform(3);
		System.out.println(p2d.toString());

		/* DEMO CODE from the JUNG Jungs!
		 * source: http://www.grotto-networking.com/JUNG/JUNG2-Tutorial.pdf
		// Graph<V, E> where V is the type of the vertices
		// and E is the type of the edges
		Graph<Integer, String> g = new SparseMultigraph<Integer, String>();
		// Add some vertices. From above we defined these to be type Integer.
		g.addVertex((Integer)1);
		g.addVertex((Integer)2);
		g.addVertex((Integer)3);
		// Add some edges. From above we defined these to be of type String
		// Note that the default is for undirected edges.
		g.addEdge("Edge-A", 1, 2); // Note that Java 1.5 auto-boxes primitives
		g.addEdge("Edge-B", 2, 3);
		// Let's see what we have. Note the nice output from the
		// SparseMultigraph<V,E> toString() method
		System.out.println("The graph g = " + g.toString());
		// Note that we can use the same nodes and edges in two different graphs.
		Graph<Integer, String> g2 = new SparseMultigraph<Integer, String>();
		g2.addVertex((Integer)1);
		g2.addVertex((Integer)2);
		g2.addVertex((Integer)3);
		g2.addEdge("Edge-A", 1,3);
		g2.addEdge("Edge-B", 2,3, EdgeType.DIRECTED);
		g2.addEdge("Edge-C", 3,2, EdgeType.DIRECTED);
		g2.addEdge("Edge-P", 2,3); // A parallel edge
		System.out.println("The graph g2 = " + g2.toString());
		*/
	}
	
	public void draw() {
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
